package com.yunkeji.dao.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.mapper.BaseMapperPlus;
import com.yunkeji.dao.domain.PayLog;
import com.yunkeji.dao.vo.PayLogVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 关注-粉丝Mapper接口
 *
 * @author 老张
 * @date 2024-05-27
 */
public interface PayLogMapper extends BaseMapperPlus<PayLogMapper, PayLog, PayLogVo> {

    @Select("select IFNULL(sum(change_num),0) from ykj_pay_log where user_id=#{uid} and log_type=#{type}")
    BigDecimal sumByType(@Param("uid") Long userId, @Param("type") Integer type);

    @Select(
        "SELECT " +
        "trace_id, " +
        "IF " +
        "( " +
        "max( log_type ) = 214  " +
        "AND min( log_type ) = 108, " +
        "sum( change_num ) - max( change_num ) * 2, " +
        "sum( change_num )  " +
        ") AS change_num, " +
        "min( log_type ) AS log_type, " +
        "min( log_date ) AS log_date, " +
        "min( create_time ) AS create_time, " +
        "'-' AS symbol, " +
        "min( balance_type ) AS balance_type " +
        "FROM " +
        "ykj_pay_log  " +
        "WHERE " +
        "user_id = #{userId} and trace_id is not null and status = 1 " +
        "AND log_date = #{logDate} " +
        "GROUP BY " +
        "trace_id " +
        "having log_type < 201 and change_num > 0 " +
        "order by create_time desc")
    Page<PayLogVo> selectPayLogByUserId(@Param("userId") Long userId, @Param("logDate") Integer logDate, @Param("page") Page<PayLog> page);


    @Select("<script>" +
        "SELECT  " +
        "c.*,  " +
        "u.sex as sex " +
        "FROM  " +
        "ykj_pay_log c  " +
        "LEFT JOIN ykj_app_user u ON c.user_id = u.id " +
        "where c.status=1 " +
        "<if test='platformId!=null'>" +
        "and c.platform_id = #{platformId} " +
        "</if>" +
        "<if test='logType!=null'>" +
        "and c.log_type in  " +
        "<foreach item='type' collection='logType' open='(' separator=',' close=')'> " +
        "#{type} " +
        "</foreach>" +
        "</if>" +
        "<if test='symbol!=null'>" +
        "and c.symbol = #{symbol} " +
        "</if>" +
        "<if test='balanceType!=null'>" +
        "and c.balance_type = #{balanceType} " +
        "</if>" +
        "<if test='userId!=null'>" +
        "and c.user_id = #{userId} " +
        "</if>" +
        "order by c.create_time desc " +
        "</script>")
    Page<PayLogVo> selectListPage(@Param("platformId") Long platformId,
                                  @Param("userId") Long userId,
                                  @Param("logType") List<Integer> logType,
                                  @Param("symbol") String symbol,
                                  @Param("balanceType") Integer balanceType,
                                  @Param("Page") Page<PayLog> Page);

}
